package com.soft.sitegenerator.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.PostListForm;

public class PostListAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		PostListForm postListForm = (PostListForm) form;

		Component component = new DataBaseComponent();
	
		User user = (User) component.getBean("usersHib");

		// Poner el Id del usuario logueado
		user.setId((Long) request.getSession().getAttribute("USER_LOGIN"));

		try {
			user.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		user.setLastPostRead(new Date());
		user.update();
		
		postListForm.setGroups(user.getNewsGroups()); 

		return mapping.findForward("showList");
	}

}